package com.net.framework.help.widget.tagview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import com.net.framework.help.R;
import com.net.framework.help.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Author：xiaobin.zhang
 * Email：154722566@qq.com
 * Date：2018-01-16 15:45
 * Desc：<--功能描述-->
 */
public class TagListView extends FlowLayout implements View.OnClickListener {
    private OnTagCheckedChangedListener mOnTagCheckedChangedListener;
    private OnTagClickListener mOnTagClickListener;
    private final List<TagData> mTags = new ArrayList<>();

    private static final int DEFAULT_TEXT_COLOR = Color.BLACK;
    private static final int DEFAULT_TEXT_BACKGROUND = R.drawable.drawable_theme_deep_bg_4dp;
    private int tagTextColor;//文字颜色
    private int tagViewBackground;//背景色
    private Drawable tagLeftDrawable;//左边投标
    private Drawable tagRightDrawable;//右边投标
    private Drawable tagClearDrawable;//删除按钮
    private boolean mIsDeleteMode;

    private int tagItemLayout;

    /**
     * @param context
     */
    public TagListView(Context context) {
        super(context);
// TODO Auto-generated constructor stub
        init(context, null);
    }

    /**
     * @param context
     * @param attributeSet
     */
    public TagListView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
// TODO Auto-generated constructor stub
        init(context, attributeSet);
    }

    /**
     * @param context
     * @param attributeSet
     * @param defStyle
     */
    public TagListView(Context context, AttributeSet attributeSet, int defStyle) {
        super(context, attributeSet, defStyle);
// TODO Auto-generated constructor stub
        init(context, attributeSet);
    }

    @Override
    public void onClick(View v) {
        if ((v instanceof TagView)) {
            TagData localTag = (TagData) v.getTag();
            if (this.mOnTagClickListener != null) {
                this.mOnTagClickListener.onTagClick((TagView) v, localTag);
            }
        }
    }

    private void init(Context context, AttributeSet attributeSet) {
        readStyleParameters(context, attributeSet);
    }

    private void readStyleParameters(Context context, AttributeSet attributeSet) {
        TypedArray a = context.obtainStyledAttributes(attributeSet, R.styleable.TagListView);
        try {
            tagTextColor = a.getColor(R.styleable.TagListView_tagTextColor, DEFAULT_TEXT_COLOR);
            tagViewBackground = a.getResourceId(R.styleable.TagListView_tagViewBackground, DEFAULT_TEXT_BACKGROUND);
            tagLeftDrawable = a.getDrawable(R.styleable.TagListView_tagLeftDrawable);
            tagRightDrawable = a.getDrawable(R.styleable.TagListView_tagRightDrawable);
            tagClearDrawable = a.getDrawable(R.styleable.TagListView_tagClearDrawable);
            mIsDeleteMode = a.getBoolean(R.styleable.TagListView_isTagDeleteMode, false);
        } finally {
            a.recycle();
        }
    }

    private void inflateTagView(final TagData t, boolean b) {
        if (tagItemLayout <= 0) {
            UIUtils.shortToast("请先通过setTagItemLayout()函数设置Tag的layout");
            return;
        }
        TextView localTagView = (TextView) View.inflate(getContext(), tagItemLayout, null);
        localTagView.setText(t.getTitle());
        localTagView.setTag(t);
        localTagView.setTextColor(tagTextColor);
        localTagView.setBackgroundResource(tagViewBackground);
//        localTagView.setChecked(t.isChecked());
//        localTagView.setCheckEnable(b);
        if (mIsDeleteMode && null != tagClearDrawable) {
            int k = (int) TypedValue.applyDimension(1, 5.0F, getContext().getResources().getDisplayMetrics());
            localTagView.setPadding(localTagView.getPaddingLeft(), localTagView.getPaddingTop(), k, localTagView.getPaddingBottom());
            localTagView.setCompoundDrawablesWithIntrinsicBounds(null, null, tagClearDrawable, null);
        }
        if ((null != tagLeftDrawable) || (null != tagRightDrawable)) {
            localTagView.setCompoundDrawablesWithIntrinsicBounds(tagLeftDrawable, null, tagRightDrawable, null);
        }
//        localTagView.setOnClickListener(this);
//        localTagView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            public void onCheckedChanged(CompoundButton paramAnonymousCompoundButton, boolean paramAnonymousBoolean) {
//                t.setChecked(paramAnonymousBoolean);
//                if (TagListView.this.mOnTagCheckedChangedListener != null) {
//                    TagListView.this.mOnTagCheckedChangedListener.onTagCheckedChanged((TagView) paramAnonymousCompoundButton, t);
//                }
//            }
//        });
        addView(localTagView);
    }

    public void addTag(String s) {
        addTag(s, false);
    }

    public void addTag(String s, boolean b) {
        addTag(new TagData(s), b);
    }

    public void addTag(TagData tag) {
        addTag(tag, false);
    }

    public void addTag(TagData tag, boolean b) {
        mTags.add(tag);
        inflateTagView(tag, b);
    }

    public void addTags(List<TagData> lists) {
        addTags(lists, false);
    }

    public void addTags(List<TagData> lists, boolean b) {
        for (int i = 0; i < lists.size(); i++) {
            addTag(lists.get(i), b);
        }
    }

    public List<TagData> getTags() {
        return mTags;
    }

    public View getViewByTag(TagData tag) {
        return findViewWithTag(tag);
    }


    public void removeTag(TagData tag) {
        mTags.remove(tag);
        removeView(getViewByTag(tag));
    }

    public void setDeleteMode(boolean b) {
        mIsDeleteMode = b;
    }

    public boolean isDeleteMode() {
        return mIsDeleteMode;
    }

    public int getTagItemLayout() {
        return tagItemLayout;
    }

    public void setTagItemLayout(int tagItemLayout) {
        this.tagItemLayout = tagItemLayout;
    }

    public void setOnTagCheckedChangedListener(
            OnTagCheckedChangedListener onTagCheckedChangedListener) {
        mOnTagCheckedChangedListener = onTagCheckedChangedListener;
    }

    public void setOnTagClickListener(OnTagClickListener onTagClickListener) {
        mOnTagClickListener = onTagClickListener;
    }


    public void setTags(List<? extends TagData> lists) {
        setTags(lists, false);
    }

    public void setTags(List<? extends TagData> lists, boolean b) {
        removeAllViews();
        mTags.clear();
        for (int i = 0; i < lists.size(); i++) {
            addTag(lists.get(i), b);
        }
    }

    public interface OnTagCheckedChangedListener {
        void onTagCheckedChanged(TagView tagView, TagData tag);
    }

    public interface OnTagClickListener {
        void onTagClick(TagView tagView, TagData tag);
    }
}